import java.util.*
import java.text.*
import groovy.sql.Sql

public class XmlToCsv extends Pacx {

    public static void main(String[] args) {
        def xmlToCsv = new XmlToCsv()
        xmlToCsv.run()
        xmlToCsv.sql.close()
    }

    
    def output_dir
    
    public XmlToCsv() {
        super()
        log = new File(config.doc4.xmltocsv.logfile)
        output_dir = config.doc4.xmltocsv.csvdir
        queries = config.doc4.xmltocsv.queries
    }
    
    def void run() {
        
        handleQueue()
    }
    
    def void handleQueue() {
        def sql1 = Sql.newInstance(dbname, dbuser, dbpasswd, dbdriver)
        
        def pkgid = 0
        sql.eachRow(queries.getmaxid) {
          ro ->
            if (ro.max != null) 
                pkgid = ro.max+1
        }
    
        log("== Converting XML packages metadata to CSV ==")
        log("== Starting identification of packages from: $pkgid ==")
        def packages_out = new File("$output_dir/packages.csv")
        def package_files_out = new File("$output_dir/package-files.csv")
        def package_dependencies_out = new File("$output_dir/package-dependencies.csv")
        packages_out.setText("")
        package_dependencies_out.setText("")
        package_files_out.setText("")
        
        sql.eachRow(queries.getqueue) {
            row ->
                def pkg = row.pkg
                def path = extractionDir+"/"+stringToInteger(pkg)+"/$pkg/metadata.xml"
                def metadatafile = new File(path)
                
                if (metadatafile.exists()) {
                    def start = sdf.format(new Date())
                    sql1.execute(queries.setrunning, [start, pkg])
                    log("=== XML to CSV: "+metadatafile.absolutePath.substring(extractionDir.length())+" ...")
                    try {
                        parse_package_metadata(metadatafile, packages_out, package_files_out, package_dependencies_out, pkgid)
                        pkgid++
                        def end = sdf.format(new Date())
                        sql1.execute(queries.setfinished, ["finished", end, "", pkg])
                    } catch (Exception e) {
                        log.append("\n*** Exception: "+e.getMessage())
                        def error = e.getMessage()
                        log(error)
                        error = error.replaceAll("'","\\\\'")
                        def end = sdf.format(new Date())
                        try {
                            
                            sql1.execute(queries.setfinished, ["failed", end, error, pkg])
                        } catch (Exception exc) {    
                            exc.printStackTrace()
                        }
                        
                    }
                } else {
                        def end = sdf.format(new Date())
                        try {
                            sql1.execute(queries.setfinished, ["failed", end, "File "+metadatafile.getAbsolutePath()+ " not found", pkg])
                        } catch (Exception exc) {    
                            exc.printStackTrace()
                        }
                        
                    
                }    
                
                
        }
    
    }
    
    def parse_package_metadata(File metadata, File packages_out, File package_files_out, File package_dependencies_out, long pkgid) throws Exception {
        def entries     = new XmlParser().parseText(metadata.text)
        def pkg         = entries.get("package")[0]
        def pkgfilename    = pkg.'@filename'
        def name        = pkg.name[0]?.text()
        def license     = pkg.license[0]?.text()
        def home        = pkg.homepage[0]?.text()
        def description = pkg.description[0]?.text().replaceAll("\"","\\\\\"")
        def scripts     = pkg.scripts[0]
        def prein       = scripts.prein[0]?.text().replaceAll("\"","\\\\\"")
        def postin      = scripts.postin[0]?.text().replaceAll("\"","\\\\\"")
        def preun       = scripts.preun[0]?.text().replaceAll("\"","\\\\\"")
        def postun      = scripts.postun[0]?.text().replaceAll("\"","\\\\\"")
        def distrib     = pkg.distribution[0]?.text()
        def version     = pkg.version[0]?.text()
        def release     = pkg.release[0]?.text()
        def branch      = pkg.branch[0]?.text()
        def section     = pkg.section[0]?.text()
        def architecture = pkg.arch[0]?.text()
        architecture = (pkgfilename != null && pkgfilename.indexOf(".src.rpm")>=0) ? "SRPMS" : architecture
        architecture = (pkgfilename != null && pkgfilename.indexOf(".noarch.rpm")>=0) ? "noarch" : architecture
    
        def group       = pkg.group[0]?.text()
        def sourcerpm   = pkg.sourcerpm[0]?.text()
        def buildhost   = pkg.buildhost[0]?.text()
        def buildtime   = pkg.buildtime[0]?.text()
        def size        = pkg.size[0].text()
        def packager    = pkg.packager[0]?.text()
        def packager_email = pkg.packager_email[0]?.text()
        def summary     = pkg.summary[0]?.text()
        def changelog   = new String(pkg.changelog[0]?.text().replaceAll("\"","\\\\\"").getBytes("UTF-8"))
    
        packages_out.append("\"$pkgid\",\"$pkgfilename\",\"$version\",\"$release\",\"$architecture\",\"$branch\",\"$section\",\"$packager_email\",\"$name\",\"$home\",\"$license\",\"$group\",\"$summary\",\"$description\",\"$changelog\",\"$sourcerpm\",\"$packager\",\"$prein\",\"$postin\",\"$preun\",\"$postun\",\"$buildhost\",\"$buildtime\",\"\",\"\",\"$size\",\"0\",NULL\n")
    
    
        for (provide in pkg.provides) {
               def item     = provide.'@name'
               def dep      = "provides"
               package_dependencies_out.append("\"$pkgid\",\"$dep\",\"$item\",\"\"\n")
        }
        for (require in pkg.requires) {
                def item    = require.'@name'
                def dep     = "requires"
                package_dependencies_out.append("\"$pkgid\",\"$dep\",\"$item\",\"\"\n")
        }
        for (conflict in pkg.conflicts) {
               def item    = conflict.'@name'
               version = conflict.'@version'
               def dep     = "conflicts"
               package_dependencies_out.append("\"$pkgid\",\"$dep\",\"$item\",\"$version\"\n")
        }
    

        for (suggests in pkg.suggests) {
               def item    = suggests.'@name'
               version = suggests.'@version'
               def dep     = "suggests"
               package_dependencies_out.append("\"$pkgid\",\"$dep\",\"$item\",\"$version\"\n")
        }
    
        for (obsoletes in pkg.obsoletes) {
               def item    = obsoletes.'@name'
               def dep     = "obsoletes"
               package_dependencies_out.append("\"$pkgid\",\"$dep\",\"$item\",\"\"\n")
        }
    
    

    
        for (pkgfile in pkg.file) {
            def path    = pkgfile.'@path'
            def type    = pkgfile.'@type'
            size    = pkgfile.'@size'
            def extra   = pkgfile.'@extra' != null ? pkgfile.'@extra' : 0 
            def cat   = pkgfile.'@cat' != null ? pkgfile.'@cat': 'package' 
            package_files_out.append("\"${pkgid}\",\"${path}\",\"${type}\",\"${size}\",\"${extra}\",\"${cat}\"\n")
        }
        
        return
    }

}